<!--
 *Author：jxx
 *Date：{Date}
 *Contact：283591387@qq.com
 *业务请在@/extension/mes/mes/ProductAssign.jsx或ProductAssign.vue文件编写
 *新版本支持vue或【表.jsx]文件编写业务,文档见:https://doc.volcore.xyz/docs/view-grid、https://doc.volcore.xyz/docs/web
 -->
<template>
    <view-grid ref="grid" :columns="columns" :detail="detail" :details="details" :editFormFields="editFormFields"
        :editFormOptions="editFormOptions" :searchFormFields="searchFormFields" :searchFormOptions="searchFormOptions"
        :table="table" :extend="extend" :onInit="onInit" :onInited="onInited" :searchBefore="searchBefore"
        :searchAfter="searchAfter" :addBefore="addBefore" :addAfter="addAfter" :updateBefore="updateBefore"
        :delBefore="delBefore" :auditModelOpenBefore="auditModelOpenBefore" :rowClick="rowClick"
        :modelOpenBefore="modelOpenBefore" :modelOpenAfter="modelOpenAfter">
        <!-- 自定义组件数据槽扩展，更多数据槽slot见文档 -->
        <template #gridHeader>
        </template>
        <template #gridFooter>
            <div v-if="isShowed">
                <div class="productProcessReport">
                    <productProcessReport ref="productProcessReportRef"></productProcessReport>
                </div>
            </div>
        </template>
    </view-grid>
</template>
<script setup lang="jsx">
import productProcessReport from './ProductProcessReport.vue'
import extend from "@/extension/mes/mes/ProductAssign.jsx";
import viewOptions from './ProductAssign/options.js'
import { ref, reactive, getCurrentInstance, watch, onMounted } from "vue";
import QrcodeVue from 'qrcode.vue'
import { useRoute } from 'vue-router'
const route = useRoute()
const grid = ref(null);
const { proxy } = getCurrentInstance()
const productProcessReportRef = ref();
//http请求，proxy.http.post/get
const { table, editFormFields, editFormOptions, searchFormFields, searchFormOptions, columns, detail, details } = reactive(viewOptions())
let isShowed = false
let gridRef;//对应[表.jsx]文件中this.使用方式一样
//生成对象属性初始化
const onInit = async ($vm) => {
    gridRef = $vm;
    gridRef.load = false
    if (route.name == "ProductAssign") {
        gridRef.load = true
        isShowed = true
        proxy.base.setItem('getAssignSelectRow', () => {
            return gridRef.getTable(true).getSelected();
        })
        proxy.base.setItem('mainSearch', () => {
            gridRef.search()
        })
    }
    //与jsx中的this.xx使用一样，只需将this.xx改为gridRef.xx
    //更多属性见：https://doc.volcore.xyz/docs/view-grid
}
//生成对象属性初始化后,操作明细表配置用到
const onInited = async () => {
    columns.forEach(x => {
        //开启表头过滤
        x.filterData = true;
    })

    gridRef.columns.splice(2, 1, {
        title: '作业任务码',
        field: 'BillNo',
        width: 100,
        align: 'center',
        render: (h, { row, column, index }) => {
            return (
                <div style="padding-top:4px">
                    {' '}
                    {/* CatalogCode改为你的字段 */}
                    <QrcodeVue value={row.BillNo} size={80} />
                </div>
            )
        }
    })
    if (route.name == "ProductAssign") {
        gridRef.height = gridRef.height - 310;
        if (gridRef.height < 200) {
            gridRef.height = 200;
        }

        gridRef.buttons.splice(3, 0, {
            name: '报工', //按钮名称
            icon: 'el-icon-document', //按钮图标:组件示例->图标
            //primary、success、warning、error、info、text、danger
            type: 'danger',
            plain: true,
            onClick: () => {

                productProcessReportAddOpen()
                // createProductAssign()
            }
        })
    }
    else {
        gridRef.height = 200
    }
}
const productProcessReportAddOpen = () => {
    let selectRows = gridRef.getSelectRows();

    if (selectRows.length == 0) {
        proxy.$message.error("请选择派工单！");
        return
    }
    if (selectRows[0].AuditStatus == 0 || selectRows[0].AuditStatus == null) {
        proxy.$message.error("该车间作业任务未审核！");
        return
    }
    if (selectRows[0].CompleteStatusId == 1) {
        proxy.$message.error("该车间作业任务已完工！");
        return
    }

    setTimeout(() => {

        productProcessReportRef.value.$refs.grid.editFormFields.ProductAssignId = selectRows[0].ProductAssignId
        productProcessReportRef.value.$refs.grid.editFormFields.ProductNoticeId = selectRows[0].ProductNoticeId
        productProcessReportRef.value.$refs.grid.editFormFields.ProductTaskProcessId = selectRows[0].ProductTaskProcessId
        productProcessReportRef.value.$refs.grid.editFormFields.AssignNo = selectRows[0].BillNo
        productProcessReportRef.value.$refs.grid.editFormFields.BillDate = proxy.base.getDate()
        productProcessReportRef.value.$refs.grid.editFormFields.AssignDate = selectRows[0].BillDate
        productProcessReportRef.value.$refs.grid.editFormFields.ProductProcessNo = selectRows[0].ProductProcessNo
        productProcessReportRef.value.$refs.grid.editFormFields.ProductProcessId = selectRows[0].ProductProcessId
        productProcessReportRef.value.$refs.grid.editFormFields.ProductProcessName = selectRows[0].ProductProcessName
        productProcessReportRef.value.$refs.grid.editFormFields.ProductProcessTypeId = selectRows[0].ProductProcessTypeId
        productProcessReportRef.value.$refs.grid.editFormFields.DepartmentId = selectRows[0].DepartmentId
        productProcessReportRef.value.$refs.grid.editFormFields.GroupId = selectRows[0].GroupId
        productProcessReportRef.value.$refs.grid.editFormFields.ReportWorkTypeId = selectRows[0].ReportWorkTypeId
        productProcessReportRef.value.$refs.grid.editFormFields.PriceTypeId = selectRows[0].PriceTypeId
        productProcessReportRef.value.$refs.grid.editFormFields.WorkHourPrice = selectRows[0].WorkHourPrice
        productProcessReportRef.value.$refs.grid.editFormFields.WorkQtyPrice = selectRows[0].WorkQtyPrice
        productProcessReportRef.value.$refs.grid.editFormFields.IsChecked = selectRows[0].IsChecked

        productProcessReportRef.value.$refs.grid.editFormFields.PlanQty = selectRows[0].PlanQty

        //待汇报数量=派工数量-汇报数量
        let reportQty = productProcessReportRef.value.$refs.grid.editFormFields.ReportQty != undefined ?
            productProcessReportRef.value.$refs.grid.editFormFields.ReportQty : 0;
        productProcessReportRef.value.$refs.grid.editFormFields.WaitReportQty = selectRows[0].AssignQty - reportQty

        productProcessReportRef.value.$refs.grid.editFormFields.ReportQty = null
        productProcessReportRef.value.$refs.grid.editFormFields.QualifyQty = 0
        productProcessReportRef.value.$refs.grid.editFormFields.UnQualifyQty = 0
        productProcessReportRef.value.$refs.grid.editFormFields.ScrapQty = 0


        //根据车间作业任务是否启用质检带到工序报工记录中 判断汇报数量 合格数量 不合格数量是否只读
        if (selectRows[0].IsChecked == 0){
            productProcessReportRef.value.$refs.grid.getFormOption("ReportQty").readonly = true
            productProcessReportRef.value.$refs.grid.getFormOption("QualifyQty").readonly = false
            productProcessReportRef.value.$refs.grid.getFormOption("UnQualifyQty").readonly = false
        }else{
            productProcessReportRef.value.$refs.grid.getFormOption("ReportQty").readonly = false
            productProcessReportRef.value.$refs.grid.getFormOption("QualifyQty").readonly = true
            productProcessReportRef.value.$refs.grid.getFormOption("UnQualifyQty").readonly = true
        }
    }, 400);
    productProcessReportRef.value.$refs.grid.add()
}

const searchBefore = async (param) => {
    //界面查询前,可以给param.wheres添加查询参数
    //返回false，则不会执行查询
    if (route.name != "ProductAssign") {
        const noticeRows = getNoticeSelectRow();
        if (!noticeRows.length) {
            proxy.$message.error('请先选择【生产通知】数据')
            return false;
        }
        //查询前获取bom的id，查询明细表
        param.wheres.push({ name: "ProductNoticeId", value: noticeRows[0].ProductNoticeId })
    }
    else {
        param.wheres.push({ name: "BillNo", value: "" })
    }

    return true;
}
const searchAfter = async (rows, result) => {
    return true;
}
const addBefore = async (formData) => {
    //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
    return true;
}
const addAfter = (result) => {  //新建保存后result返回的状态及表单对象
    refreshMainSearch()
    return true;//返回false不会关闭弹出框
}
const updateBefore = async (formData) => {
    //编辑保存前formData为对象，包括明细表、删除行的Id
    return true;
}
const delBefore = (ids, rows) => { //查询界面的表删除前 ids为删除的id数组,rows删除的行
    let isDeleted = true
    rows.forEach(row => {
        if (row.AuditStatus != 0 && row.AuditStatus != null) {
            isDeleted = false
            proxy.$message.error(row.BillNo + "已审核不允许删除！");
            return isDeleted;
        }
    });
    return isDeleted;//返回false，界面上不会显示
}
const auditModelOpenBefore = (rows, isAnit) => {//审核前
    //rows:审批选中的行数据
    let selectRows = gridRef.getSelectRows()
    let flag = false
    if (isAnit) {
        selectRows.forEach(row => {
            if (row.QualifyQty > 0 || row.UnQualifyQty > 0 || row.ScrapQty > 0) {
                return proxy.$message.error(row.BillNo + "已报工，不允许反审核！");
            }
            else {
                flag = true;
            }
        });
    }
    else {
        flag = true;
    }

    return flag;
}
const rowClick = ({ row, column, event }) => {
    //查询界面点击行事件
    // grid.value.toggleRowSelection(row); //单击行时选中当前行;
    grid.value.clearSelection();
    //点击行默认选中
    grid.value.toggleRowSelection(row); //单击行时选中当前行;
    productProcessReportRef.value.$refs.grid.search();

}
const getNoticeSelectRow = () => {
    const fn = proxy.base.getItem('getNoticeSelectRow');
    return fn();
}
const refreshMainSearch = () => {
    const fn = proxy.base.getItem('mainSearch');
    return fn();
}
const modelOpenBefore = async (row) => {//弹出框打开后方法
    return true;//返回false，不会打开弹出框
}
const modelOpenAfter = (row) => {

    gridRef.setFormReadonly(false);

    if (gridRef.currentAction == 'Add') {
        gridRef.editFormFields.BillDate = proxy.base.getDate();
        gridRef.boxButtons.forEach((btn) => {
            if (btn.name == '保存') {
                btn.hidden = false;
                //或者设置只读
                // btn.readonly = true;
            }

        });
    }
    else {
        if (row.AuditStatus != 0) {
            gridRef.setFormReadonly(true);
            gridRef.boxButtons.forEach((btn) => {
                if (btn.name == '保存') {
                    btn.hidden = true;
                    //或者设置只读
                    // btn.readonly = true;
                }

            });
        }
        else {
            gridRef.boxButtons.forEach((btn) => {
                if (btn.name == '保存') {
                    btn.hidden = false;
                    //或者设置只读
                    // btn.readonly = true;
                }
            });

        }
    }
}
//监听表单输入，做实时计算
//watch(() => editFormFields.字段,(newValue, oldValue) => {	})
//对外暴露数据
defineExpose({})
</script>
